﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Win32;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;

namespace dotNetwork
{
  public class Logs
  {
    public enum Trace32Type { Information = 1, Warning = 2, Error = 3, Question = 4 }
    public bool gDebug = false;

    public void WriteLogFile(string pText)
    {
        WriteLogFile(pText, "", Trace32Type.Information);
    }

    public void WriteLogFile(string pText, string pComponent)
    {
      WriteLogFile(pText, pComponent, Trace32Type.Information);
    }

    public void WriteLogFile(string pText, string pComponent, Trace32Type pType)
    {
      try
      {
        string sFolder = Directory.GetCurrentDirectory();

        if (sFolder.Substring(sFolder.Length - 1) != "\\")
          sFolder += "\\";

        string sLogFile = sFolder + "ConfigMgrRegistrationRequest.log";
        DateTime dToday = DateTime.Now;

        string sLine = "<![LOG[" + pText + "]LOG]!>";
        sLine += "<";
        sLine += "time=\"" + dToday.Hour.ToString() + ":" + dToday.Minute.ToString() + ":" + dToday.Second.ToString() + "." + dToday.Millisecond.ToString() + "+-000\" ";
        sLine += "date=\"" + dToday.Month.ToString() + "-" + dToday.Day.ToString() + "-" + dToday.Year.ToString() + "\" ";
        sLine += "component=\"" + pComponent + "\" ";
        sLine += "context=\"\" ";
        sLine += "type=\"" + ((int)pType).ToString() + "\" ";
        sLine += "thread=\"0\" ";
        sLine += "file=\"\"";
        sLine += ">";

        StreamWriter w;
        if (File.Exists(sLogFile))
          w = File.AppendText(sLogFile);
        else
          w = File.CreateText(sLogFile);

        w.WriteLine(sLine);
        w.Flush();
        w.Close();
      }
      catch
      {
      }
    }

    public void WriteDebugLog(string pText)
    {
      if (gDebug)
        WriteLogFile(pText, "Debug", Logs.Trace32Type.Information);
    }

    public void WriteDebugLog(string pText, bool pAddExtraInformation)
    {
      if (gDebug)
      {
        WriteLogFile("====Start Exception====", "Debug", Logs.Trace32Type.Information);
        WriteLogFile(pText, "Debug", Logs.Trace32Type.Information);

        StackFrame sf = null;
        StackTrace st = new StackTrace();
        Type thisType = GetType();
        foreach (StackFrame sfi in st.GetFrames())
        {
          sf = sfi;
          if (sf != null)
          {
            string sReturn = sf.GetMethod().Name;

            string sParams = "(";
            foreach (System.Reflection.ParameterInfo _param in sf.GetMethod().GetParameters())
            {
              if (sParams != "(")
                sParams += ", ";

              sParams += _param.ParameterType.ToString() + " " + _param.Name;
            }
            WriteLogFile(sReturn + sParams + ")", "Debug", Logs.Trace32Type.Information);
          }
        }
        WriteLogFile("====End Exception====", "Debug", Logs.Trace32Type.Information);
      }
    }

    public string GetProcedureName()
    {
      StackFrame sf = null;
      StackTrace st = new StackTrace(1, true);
      Type thisType = GetType();
      foreach (StackFrame sfi in st.GetFrames())
      {
        Type callType = sfi.GetMethod().DeclaringType;
        if (callType != thisType && callType.Namespace == thisType.Namespace && !callType.IsInterface)
        {
          sf = sfi;
          break;
        }
      }
      if (sf != null)
      {
        string sReturn = sf.GetMethod().Name;
        //System.Reflection.MethodBase.GetCurrentMethod().GetParameters() 
        //MethodBase mb = sf.GetMethod();

        //MethodBody db = sf.GetMethod().GetMethodBody();
        //sf.GetMethod().GetMethodBody().GetILAsByteArray()[2]
        //sf.GetMethod().getre

        //var ilReader = new ILReader(method);
        //var allInstructions = ilReader.ToArray();

        //foreach (LocalVariableInfo lvi in db.LocalVariables)
        //  sReturn += lvi.ToString();
        //          lvi.ToString();

        string sParams = "(";
        foreach (System.Reflection.ParameterInfo _param in sf.GetMethod().GetParameters())
        {
          if (sParams != "(")
            sParams += ", ";

          sParams += _param.ParameterType.ToString() + " " + _param.Name;
        }

        return sReturn + sParams + ")";
      }
      else
        return "";
    }
  }
}
